
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
   
    address_list:[],
    num:0,
    is_show_time:1,
    fromPage: '' // 来源页面，用于判断是选择地址还是管理地址
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    new app.ToastPannel();
    var that = this
    that.requests()
    that.setData({ fromPage: options.from || '' });
  },
  onShow() {
    var that = this
  	that.requests()
  },

  requests:function(){
    var that = this
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Buyer_User&met=address&typ=json',
      method: 'GET',
      data: { k: key, u: uid },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
     
        if (res.data.status == 200) {
          var num = 0
          for (var i in res.data.data.address_list) {
            num++
          }
          that.setData({
            address_list: res.data.data.address_list,
            num:num,
            is_show_time:0
          })
        } else {
          app.show('数据加载失败', 'loading')
          
        }
      }
    })
  },
  

  //地址选择
  address:function(e){
    var address_id = e.currentTarget.dataset.address_id
    var user_address_contact = e.currentTarget.dataset.user_address_contact
    var user_address_phone = e.currentTarget.dataset.user_address_phone
    var user_address_area = e.currentTarget.dataset.user_address_area
    var user_address_address = e.currentTarget.dataset.user_address_address
    var user_address_default = e.currentTarget.dataset.user_address_default
	var navigationAddress = e.currentTarget.dataset.user_address_navigation
	var latitude = e.currentTarget.dataset.user_address_latitude
    var longitude = e.currentTarget.dataset.user_address_longitude
	if (this.data.fromPage === 'select') {
		const address = user_address_area + user_address_address
		this.sendAddressBack(address,user_address_phone,navigationAddress,latitude,longitude)
	} else if(this.data.fromPage === 'vr_buy_step1') {
		wx.redirectTo({
		  url: '../vr_buy_step1/vr_buy_step1?address_id=' + address_id + '&user_address_contact=' + user_address_contact + '&user_address_phone=' + user_address_phone + '&user_address_area=' + user_address_area + '&user_address_address=' + user_address_address + '&user_address_default=' + user_address_default
		})
	} else {
		wx.redirectTo({
		  url: '../buy_step1/buy_step1?address_id=' + address_id + '&user_address_contact=' + user_address_contact + '&user_address_phone=' + user_address_phone + '&user_address_area=' + user_address_area + '&user_address_address=' + user_address_address + '&user_address_default=' + user_address_default
		})
	}
  },
   // 将地址数据返回给原页面
  sendAddressBack(address,phone,navigationAddress,latitude,longitude) {
		// 尝试通过事件通道发送数据
          const eventChannel = this.getOpenerEventChannel();
          if (eventChannel && eventChannel.emit) {
            eventChannel.emit('acceptAddress', address);
            eventChannel.emit('acceptPhone', phone);
            eventChannel.emit('acceptNavigationAddress', navigationAddress);
            eventChannel.emit('acceptLatitude', latitude);
            eventChannel.emit('acceptLongitude', longitude);
            wx.navigateBack();
            return;
          }
  },
  
  add_address:function(){
	  if (this.data.fromPage === 'select') {
		wx.navigateTo({
		  url: '../address_opera/address_opera?fromto=1&from=select',
		})
	}  else if(this.data.fromPage === 'vr_buy_step1') {
		wx.navigateTo({
			url: '../address_opera/address_opera?fromto=1&from=vr_buy_step1',
		})
	} else {
		wx.navigateTo({
			url: '../address_opera/address_opera?fromto=1',
		})
		
	}
  }

})